package fr.bruno.puzzlefrance;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class ImageViewMove extends ImageView {

	private static final int HEIGHT_MENU = 56;
	private float destX;
	private float destXmin;
	private float destXmax;

	private float destY;
	private float destYmin;
	private float destYmax;

	private final int DELTA = 5;

	private boolean blocked = false;

	public ImageViewMove(Context context) {
		super(context);
		Log.i("HHH", "HHH");
		setOnTouchListener(onTouch());
	}

	public ImageViewMove(Context context, AttributeSet attrs) {
		super(context, attrs);

		Log.i("HHH", "HHH" + attrs);
		setOnTouchListener(onTouch());

		for (int i = 0; i < attrs.getAttributeCount(); i++) {
			Log.i("HHH", "HHH " + i + " => " + attrs.getAttributeValue(i));
		}

		destX = attrs.getAttributeIntValue(
				"http://schemas.android.com/apk/res/fr.bruno.puzzlefrance", "destX",
				-1);
		destY = attrs.getAttributeIntValue(
				"http://schemas.android.com/apk/res/fr.bruno.puzzlefrance", "destY",
				-1);

		Log.i("HHH", "destX : " + destX);
		Log.i("HHH", "destY : " + destY);

		destXmin = destX - DELTA;
		destXmax = destX + DELTA;

		destYmin = destY - DELTA;
		destYmax = destY + DELTA;

	}

	public ImageViewMove(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.i("HHH", "HHH");
		// setOnClickListener(onClick());
		setOnTouchListener(onTouch());
	}

	private OnTouchListener onTouch() {
		return new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if (blocked) {
					return false;
				}

				Log.i("HHH", "---> " + event.getAction());
				if (MotionEvent.ACTION_MOVE == event.getAction()) {

					String msg = null;

					float x = getX();
					float y = getY() + HEIGHT_MENU;

					float xCenter = x + getHalfX();
					float yCenter = y + getHalfY();

					float xNew = event.getRawX() - getHalfX();
					float yNew = event.getRawY() - getHalfY() - HEIGHT_MENU;

					Log.i("HHH", "----------------" + getName()
							+ "----------------");

					msg = String.format("RAW : (%.0f, %.0f)", event.getRawX(),
							event.getRawY());
					Log.i("HHH", msg);

					msg = String.format("TOP : (%.0f, %.0f)", x, y);
					Log.i("HHH", msg);

					msg = String.format("CENTER : (%.0f, %.0f)", xCenter,
							yCenter);
					Log.i("HHH", msg);

					msg = String.format("NEW : (%.0f, %.0f)", xNew, yNew);
					Log.i("HHH", msg);

					setX(xNew);
					setY(yNew);

					msg = String.format(
							"X RAW ( %.0f) DOIT ETRE ENTRE (%.0f < %.0f)",
							event.getRawX(), destXmin, destXmax);
					Log.i("HHH", msg);

					msg = String.format(
							"Y RAW ( %.0f) DOIT ETRE ENTRE (%.0f < %.0f)",
							event.getRawY(), destYmin, destYmax);
					Log.i("HHH", msg);

					if ((event.getRawX() > destXmin)
							&& (event.getRawX() < destXmax)
							&& (event.getRawY() > destYmin)
							&& (event.getRawY() < destYmax)) {
						blocked = true;
						Log.i("HHH", "BLOQUED");

						setX(destX - getHalfX());
						setY(destY - getHalfY() - HEIGHT_MENU);

						generateSound();

					} else {
						Log.i("HHH", "NOT BLOQUED");
					}

				}

				return true;
			}

		};
	}

	private void pause(long ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			Log.e("HHH", e.getMessage(), e);
		}
	}

	private void generateSound() {
		try {
			Uri soundUri = RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			MediaPlayer mMediaPlayer = new MediaPlayer();
			mMediaPlayer.setDataSource(getContext(), soundUri);
			final AudioManager audioManager = (AudioManager) getContext()
					.getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
				mMediaPlayer
						.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
				mMediaPlayer.setLooping(true);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
				pause(1000);
				mMediaPlayer.stop();
			}
		} catch (Exception e) {
			Log.e("HHH", e.getMessage(), e);
		}
	}

	private int getHalfX() {
		return (int) getImageView().getWidth() / 2;
	}

	private int getHalfY() {
		return (int) getImageView().getHeight() / 2;
	}

	private String getName() {
		return getResources().getResourceName(getImageView().getId());
	}

	private ImageView getImageView() {
		return this;
	}

	public float getDestX() {
		return destX;
	}

	public void setDestX(float destX) {
		this.destX = destX;
	}

	public float getDestY() {
		return destY;
	}

	public void setDestY(float destY) {
		this.destY = destY;
	}

}
